// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import MenuUtils from './utils/MenuUtils'
import './assets/css/breadcrumb-style.css'
import 'babel-polyfill'
import myBread from './components/common/myBread'
import axios from 'axios'

Vue.config.productionTip = false
Vue.use(Element)
Vue.prototype.$ajax = axios
// //www.gztang.cn/admin/manager/  //192.168.0.110/admin/manager/
axios.defaults.baseURL = 'https://www.gztang.cn'
axios.defaults.withCredentials = true
axios.defaults.headers['Content-Type'] = 'application/json'
Vue.component('myBread', myBread)

axios.interceptors.response.use(function (response) {
  if (!response.data.success) {
    Element.Message.error(response.data.errorMessage)
  }
  return response
}, function (error) {
  return Promise.reject(error)
})

let data = JSON.parse(window.sessionStorage.getItem('user'))
if (data) {
  let routes = []
  MenuUtils(routes, data)
  router.addRoutes(routes)
  window.sessionStorage.removeItem('isLoadNodes')
}
router.beforeEach((route, redirect, next) => {
  // console.log(route, redirect)
  let data = JSON.parse(window.sessionStorage.getItem('user'))
  // if (data && route.path === '/login') {
  //   window.sessionStorage.removeItem('user')
  //   window.sessionStorage.removeItem('isLoadNodes')
  //   window.location.href = '/'
  //   return false
  // }
  if (!data && route.path !== '/') {
    next({name: 'notFind'})
  } else {
    if (route.path) {
      next()
    } else {
      next({path: '/'})
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
